//
//  MBProgressHUD+MJ.m
//
//  Created by mj on 13-4-18.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import "MBProgressHUD+MJ.h"

@implementation MBProgressHUD (MJ)
#pragma mark 显示信息
+ (void)show:(NSString *)text icon:(NSString *)icon view:(UIView *)view
{
    if (view == nil) view = [UIApplication sharedApplication].keyWindow;
    // 快速显示一个提示信息
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.labelText = text;
    
    if (!icon) {
        hud.label.font = [UIFont boldSystemFontOfSize:15.0];
    }else
    {
        // 设置图片
        hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"MBProgressHUD.bundle/%@", icon]]];
    }
    
    // 再设置模式
    hud.mode = MBProgressHUDModeCustomView;
    
    // 隐藏时候从父控件中移除
    hud.removeFromSuperViewOnHide = YES;
    
    // 1秒之后再消失
    [hud hide:YES afterDelay:2];
}

#pragma mark 显示错误信息
+ (void)showError:(NSString *)error toView:(UIView *)view{
    [self show:error icon:@"error.png" view:view];
}

+ (void)showSuccess:(NSString *)success toView:(UIView *)view
{
    [self show:success icon:@"success.png" view:view];
}

+ (void)showText:(NSString *)text toView:(UIView *)view
{
    
    [self show:text icon:nil view:view];
    
}

#pragma mark 显示一些信息
+ (MBProgressHUD *)showMessage:(NSString *)message toView:(UIView *)view netWork:(BOOL)isNetWork mask:(BOOL)isMask{
    if (view == nil) view = [[[UIApplication sharedApplication] keyWindow].subviews firstObject];
    // 快速显示一个提示信息
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
//    hud.label.text = message;
    // 隐藏时候从父控件中移除
//    hud.removeFromSuperViewOnHide = YES;
    
    hud.square = YES;
    hud.backgroundColor = CSClearColor;
    //    hud.mode = MBProgressHUDModeIndeterminate;
    
    //如果需要自定义的图片的话
    hud.mode = MBProgressHUDModeCustomView;
    
    if (Iphone4 || Iphone5) {
        
        hud.minSize = CGSizeMake(80, 80);
    }
    else{
        
        hud.minSize = CGSizeMake(100, 100);
    }
    
    //判断是否开启状态栏指示器
    [UIApplication sharedApplication].networkActivityIndicatorVisible = isNetWork;
    
    //自定义动画
    UIImageView *gifImageView = [[UIImageView alloc] initWithImage:[[UIImage imageNamed:@"loading0000"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    NSMutableArray *arrM = [[NSMutableArray alloc] init];
    for (int i = 0; i < 35; i ++) {
        
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"loading00%02d", i + 1]];
        [arrM addObject:image];
    }
    [gifImageView setAnimationImages:arrM];
    [gifImageView setAnimationDuration:1.5];
    [gifImageView setAnimationRepeatCount:MAXFLOAT];
    [gifImageView startAnimating];
    
    hud.customView = gifImageView;
    
    return hud;
}

+ (void)showMessage:(NSString *)message toView:(UIView *)view
{
    [self showMessage:message toView:view netWork:NO mask:NO];
}

+ (void)showMessage:(NSString *)message toView:(UIView *)view mark:(BOOL)mark
{
    [self showMessage:message toView:view netWork:NO mask:mark];
}

+ (void)showSuccess:(NSString *)success
{
    [self showSuccess:success toView:nil];
}

+ (void)showError:(NSString *)error
{
    [self showError:error toView:nil];
}

+ (void)showText:(NSString *)text toview:(UIView *)view
{
    [self showText:text toView:view];
}
+ (void)showText:(NSString *)text
{
    [self showText:text toView:nil];
}

+ (MBProgressHUD *)showMessage:(NSString *)message
{
    return [self showMessage:message toView:nil netWork:NO mask:NO];
}

+ (void)hideHUDForView:(UIView *)view
{
    [self hideHUDForView:view animated:YES];
}

+ (void)hideHUD
{
    [self hideHUDForView:nil];
}
+ (void)hideHUDNoAnimation
{
    [self hideHUDForView:nil animated:NO];
}

@end
